import request from "@/services/interceptors";
import { ObjectiveTopicType } from "@/types";
import { enumToObject, QuestionTypeEnum } from "@/utils/enums";
import { ProDescriptions } from "@ant-design/pro-components";
import type { ProDescriptionsActionType } from "@ant-design/pro-components";
import { useRef } from "react";
import { useAtomValue } from "jotai";
import { enumValuesAtom } from "@/store/enum";

interface ObjectDetailProps {
  questionId: number;
}

const ObjectDetail = ({ questionId }: ObjectDetailProps) => {
  const actionRef = useRef<ProDescriptionsActionType>(null);
  const { courseList } = useAtomValue(enumValuesAtom);

  return (
    <ProDescriptions<ObjectiveTopicType>
      actionRef={actionRef}
      request={async () => {
        const res = await request.sgks.questionGetList({ questionId });
        return res;
      }}
      column={1}
      columns={[
        {
          title: "题目编号",
          dataIndex: "questionCode",
        },
        {
          title: "题目类型",
          dataIndex: "questionType",
          valueEnum: enumToObject(QuestionTypeEnum),
        },
        {
          title: "题干",
          dataIndex: "questionStem",
        },
        {
          title: "选项(仅选择题)",
          dataIndex: "options",
          renderText: (options) => {
            return (
              <div>
                {options?.map((option: any) => {
                  return (
                    <div>
                      {option.optionSign}:{option.optionContent}
                    </div>
                  );
                })}
              </div>
            );
          },
        },
        {
          title: "正确答案",
          dataIndex: "answerChoice",
          renderText: (_, record: any) => {
            if (record.questionType === QuestionTypeEnum.选择题) {
              return record.answerChoice;
            }
            if (record.questionType === QuestionTypeEnum.填空题) {
              return record.answerContent;
            }
            if (record.questionType === QuestionTypeEnum.判断题) {
              return record.answerTf === "1" ? "对" : "错";
            }
            if (record.questionType === QuestionTypeEnum.问答题) {
              return record.answerContent;
            }
          },
        },
        {
          title: "课程",
          dataIndex: "courseId",
          valueType: "select",
          fieldProps: {
            options: courseList,
          },
        },
        {
          title: "标签",
          dataIndex: "labels",
          renderText: (labels) => {
            return labels?.map((label: any) => label?.labelName)?.join(",");
          },
        },
        {
          title: "答案解析",
          dataIndex: "answerAnalysis",
        },

        {
          title: "创建时间",
          dataIndex: "createTime",
        },
        {
          title: "更新时间",
          dataIndex: "updateTime",
        },
        {
          title: "创建人",
          dataIndex: "createBy",
        },
        {
          title: "更新人",
          dataIndex: "updateBy",
        },
      ]}
    ></ProDescriptions>
  );
};
export default ObjectDetail;
